package cn.lzj.org.service.impl;

import cn.lzj.basic.service.impl.BaseServiceImpl;
import cn.lzj.org.domain.Department;
import cn.lzj.org.mapper.DepartmentMapper;
import cn.lzj.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

//开启业务层的支持
@Service
//只读事务,全局默认是如果Bean调用事务就执行,如果没有就不用事务
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    //注入mapper层
    private DepartmentMapper departmentMapper;
    /**
     * 必须掌握查询无限极数的方案
     *    1.一次性查询出所有数据，放到内存 all
     *    2.从所有数据中找出第一层级 firstCourseType   我们需要返回的数据
     *    3.只要不是第一层级的，找自己的上级，并将自己放入上级的children中
     *    4.简单来说,就是先判断自己是不是第一层级,如果不是,就找到自己的上级,将自己放到上级的部门children中
     *
     * @return
     */
/*    @Override
    public void add(Department department) {

        System.out.println(department);
        departmentMapper.save(department);
    }*/




    @Override
    public List<Department> querytree() {
        List<Department> alldepts = departmentMapper.loadAll();
        //流式编程
        /*Map<String, Department> deptMap = alldepts.stream().collect(Collectors.toMap(Department::getId, dept -> dept));
        */
        HashMap<Long,Department> deptMap=new HashMap<>();
        for(Department deptTmp:alldepts){
            deptMap.put(deptTmp.getId(),deptTmp);
        }
        //查询出所有的数据,放入内存all里面
        List<Department> fisrtdept=new ArrayList<>();
        for (Department deptTmp:alldepts){
        //代表顶级部门
        if(deptTmp.getParent_id()==null){
            //从所以数据中找出第一层即最高级部门   fisrtdept  这是需要返回的数据
        fisrtdept.add(deptTmp);
        }else{
            //如果不是第一层级 的目录,就找到自己的上级,并将自己放入上级的子级部门中
            Department parent = deptMap.get(deptTmp.getParent_id());
            //判断父级是否为空---有无父级部门
            if(parent!=null) {
                parent.getChildren().add(deptTmp);
            }
        }
    }
        return fisrtdept;
    }

    /**
     * 添加时对部门路径的处理
     * @param department
     */
    @Override
    public void add(Department department) {
        departmentMapper.save(department);
        String dirPath="";
        if(department.getParent_id()==null){
            Long id = department.getId();
            dirPath="/"+id;
        }else {
            Long parent_id = department.getParent_id();
            //获取上级部门的路径dirpath
            String parentDirPath = departmentMapper.loadById(parent_id).getDirPath();
            dirPath = parentDirPath + "/" + department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }
    @Override
    public void update(Department department) {
        String dirPath="";
        if(department.getParent_id()==null){//没有选择上级部门  或者  选了之后删掉了
            Long id = department.getId();
            dirPath="/"+id;
        }else {
            Long parent_id = department.getParent_id();
            //获取上级部门的路径dirpath
            String parentDirPath = departmentMapper.loadById(parent_id).getDirPath();
            dirPath = parentDirPath + "/" + department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }
}
